//
//  AppIntroduceBackView.m
//  Application
//
//  Created by 薛小刚 on 2017/12/22.
//  Copyright © 2017年 薛小刚. All rights reserved.
//

#import "AppIntroduceBackView.h"
#import "UIView+BlockGesture.h"

#define BOTTON 80

@interface AppIntroduceBackView()<UIScrollViewDelegate>

@property (nonatomic, assign) CGRect myFrame;

@property (nonatomic, strong) UIScrollView * scrollView;

@property (nonatomic, strong) UIPageControl * pageControl;

@end

@implementation AppIntroduceBackView

- (instancetype) initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        _myFrame = frame;
        [self creatUI];
    }
    return self;
}

- (UIScrollView *) scrollView {
    if (_scrollView == nil) {
        _scrollView = [[UIScrollView alloc] initWithFrame:_myFrame];
        [self addSubview:_scrollView];
        _scrollView.showsVerticalScrollIndicator = NO;
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.pagingEnabled = YES;
        _scrollView.delegate = self;
    }
    return _scrollView;
}

- (UIPageControl *) pageControl {
    if (_pageControl == nil) {
        _pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 20)];
    }
    return _pageControl;
}

- (void) creatUI {
    NSArray * imageArray;
    if (_myFrame.size.height > 800) {
        imageArray = @[@"introduce_one_x", @"introduce_two_x", @"introduce_three_x"];
    } else {
        imageArray = @[@"introduce_one", @"introduce_two", @"introduce_three"];
    }
    
    for (int i = 0; i < imageArray.count; i++) {
        UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(_myFrame.size.width * i, 0, _myFrame.size.width, _myFrame.size.height)];
        [self.scrollView addSubview:imageView];
        imageView.image = [UIImage imageNamed:imageArray[i]];
        if (i == imageArray.count - 1) {
//            UIImageView * imageViewB = [[UIImageView alloc] initWithFrame:CGRectMake(_myFrame.size.width * i, 0, _myFrame.size.width, _myFrame.size.height)];
//            [self.scrollView addSubview:imageViewB];
//            imageViewB.image = [UIImage imageNamed:@"introduce_bg"];
//            imageViewB.contentMode = UIViewContentModeBottom;
//            imageViewB.userInteractionEnabled = YES;
            
            UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
            [button setBackgroundImage:[UIImage imageNamed:@"introduce_button"] forState:UIControlStateNormal];
            button.frame = CGRectMake(_myFrame.size.width * i, 0, 130, 41);
            [self.scrollView addSubview:button];
            [button setTitle:@"立即体验" forState:UIControlStateNormal];
            [button setTitleColor:xgTextDarkColor forState:UIControlStateNormal];
            button.titleLabel.font = xgSystemFont(20);
            button.center = CGPointMake(SCREEN_WIDTH / 2. + _myFrame.size.width * i, SCREEN_HEIGHT_X - BOTTON);
            [button addTapActionWithBlock:^(UIGestureRecognizer *gestureRecoginzer) {
                if (self.delegate && [self.delegate respondsToSelector:@selector(startApplication)]) {
                    [self.delegate startApplication];
                }
            }];
        }
    }
    self.scrollView.contentSize = CGSizeMake(_myFrame.size.width * imageArray.count, _myFrame.size.height);
    
    [self addSubview:self.pageControl];
    self.pageControl.centerY = SCREEN_HEIGHT_X - BOTTON + 50;
    self.pageControl.numberOfPages = imageArray.count;
    self.pageControl.currentPage = 0;
    self.pageControl.pageIndicatorTintColor = xgTextLightColor;
    self.pageControl.currentPageIndicatorTintColor = xgRedColor;
    [self.pageControl addTarget:self action:@selector(pageTap:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    int pageNum = scrollView.contentOffset.x / _myFrame.size.width;
    self.pageControl.currentPage = pageNum;
}

- (void)pageTap:(UIPageControl *) page {
    [UIView animateWithDuration:0.2 animations:^{
        self.scrollView.contentOffset = CGPointMake(page.currentPage * _myFrame.size.width, 0);
    }];
}


- (void) buttonClicked:(UIButton *) btn {
    if (self.delegate && [self.delegate respondsToSelector:@selector(startApplication)]) {
        [self.delegate startApplication];
    }
}

@end
